package Beans;

import TechnicalServices.Database;
import TechnicalServices.SQLDatabase;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;



/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author sa11
 */
public class LoggedInBean implements java.io.Serializable {

    private static LoggedInBean loggedInBean = new LoggedInBean();
    private String username = null;
    private String alias;
    private String password;

    public static synchronized LoggedInBean getInstance() {
        if (loggedInBean == null){
            loggedInBean = new LoggedInBean();
        }
    return loggedInBean;
    }
        

    public String getusername() {
        return this.username;
    }

    public void setusername(final String name) {
        this.username = name;
    }

    public String getPassword() {
        return this.password;
    }

    public void setPassword(final String password) {
        this.password = password;
    }
    
    public String getAlias() {
        return this.alias;
    }
    
    public void getAliasFromDBT(String username) {
        try {
            Database databas = new SQLDatabase("jdbc:sqlserver://idasql-db.hb.se:56001; databaseName=dbtht1117 ", "dbtht1117", "panwer09");
            databas.loadDriver();
            databas.connect();
            databas.executeSQL("SELECT anvandarID, anvandarAlias FROM Anvandare WHERE '" + username + "' = anvandarID;");

            ResultSet set = databas.getResultSet();
            if(set.next()){
                alias = set.getString(2);
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(LoggedInBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            System.err.println(ex);
        }
    }

    public void setAlias(final String alias) {
        this.alias = alias;
    }
    
    public void saveBean(final LoggedInBean saveBean) {
        this.loggedInBean = saveBean;
    }
}
